#include <lversion.h>
#if L_VERSION_MODULE_COLLISION_RESULT == 120130

#include "_lcollisionResult_120130.h"

using namespace llib;
using namespace llib::util;
using namespace llib::geom;
using namespace llib::collision;

#include <vector>
using namespace std;

void CollisionResult::AddPair( MeshFace& first, MeshFace& second )
{
    pair<MeshFace,MeshFace> pair(first,second);
    if(pair.first > pair.second) swap(pair.first, pair.second);
    _pairSet.insert(pair);
    _faceMap[pair.first] = 0;
    _faceMap[pair.second] = 0;
}

void CollisionResult::Collect()
{
    Int32 faceNum = (Int32)_faceMap.size(), p = 0;
    vector<MeshFace> localToFace(faceNum);
    for(map<MeshFace,Int32>::iterator i = _faceMap.begin(); i != _faceMap.end(); ++i) {
        i->second = p;

        MeshFace face = i->first;

        localToFace[p] = i->first;
        ++p;
    }
    DisjointSet ds(faceNum);
    Int32 firstLocalId, secondLocalId;
    for(set< pair<MeshFace,MeshFace> >::iterator i = _pairSet.begin(); i != _pairSet.end(); ++i) {

        MeshFace face0 = i->first;
        MeshFace face1 = i->second;

        firstLocalId = _faceMap[ i->first ];
        secondLocalId = _faceMap[ i->second ];
        ds.Merge(firstLocalId, secondLocalId);
    }
    IrregularTable<Int32>* itI = ds.GetGroups();

    delete _collisionGroup;
    _collisionGroup = new IrregularTable<MeshFace>(itI->GetRowSize());
    FOR(r,0,itI->size()) {
        IrregularTable<Int32>::Row& row = (*itI)[r];
        FOR(c,0,row.size())
            (*_collisionGroup)[r][c] = localToFace[ row[c] ];
    }

    delete itI;
}

#endif
